/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlet;

import donnees.User;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Emeline JUGE
 */
public class Authentification extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String userId = request.getParameter("user");
            String userPass = request.getParameter("password");
            User utilisateur = new User(userId, userPass);

            if (utilisateur.validate()) {
                request.getSession(true).setAttribute("user", utilisateur);
                out.println("<h2> Bonjour " + userId + "</h2><div><a href =\"monitor_operations.jsp\">Monitor</a></div>");
            } else {
                out.println("<h2>Mauvais login ou mot de passe !!</h2><div><a href =\"login.jsp\">Reesayer</a></div>");
            }

        } finally {
            out.close();
        }

    }

}
